package com.pan.games.play;

/**
 * @author by panstark
 * @description
 * @notice
 * @date 2021/2/28
 */
public class PrintABC {


    Object lockA = new Object();
    Object lockB = new Object();
    Object lockC = new Object();

    public synchronized void printCharactor(String name){

        System.out.println("print name :" + name);
    }

    public void pringABC(){

        Thread a = new Thread(new Print(lockC,lockA,"A"));
        Thread b = new Thread(new Print(lockA,lockB,"B"));
        Thread c = new Thread(new Print(lockB,lockC,"C"));


        try {
            a.start();
            Thread.sleep(10);
            b.start();
            Thread.sleep(10);
            c.start();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        PrintABC printABC = new PrintABC();
        printABC.pringABC();
    }

}

class Print implements Runnable{

    int circleSize = 0;

    Object prelock;
    Object currentlock;

    String name;

    Print(Object prelock,Object currentlock,String name){
        this.prelock = prelock;
        this.currentlock = currentlock;
        this.name = name;
    }


    @Override
    public void run() {

        while(circleSize<20){
            circleSize++;
            synchronized (prelock){
                synchronized (currentlock){
                    System.out.println("name:"+name);
                    currentlock.notifyAll();
                }

                try {
                    prelock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }
    }
}